package com.dbpediamobile;


import java.io.IOException;
import com.dbpediamobile.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class DBPediaMobile extends MapActivity {
	public static EditText txtInput;
	private ImageButton button1, button2, button3;
	private static final String MEDIA = "media";
	public String urlMap=null;
	public static BookmarksItemMap bip=null;
	
	 
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String input = "";
        //restauramos el input (string) guardado
        if (savedInstanceState != null) {
        	input = savedInstanceState.getString("INPUT_STRING");
        }
    	CheckConexion c = new CheckConexion();
    	
    	try {
			 if(c.check()!=""){
				 setContentView(R.layout.main);
				 button1 = (ImageButton)findViewById(R.id.button_busq_simple);
			     button1.setOnClickListener(busq_simple);
			     button2 = (ImageButton)findViewById(R.id.button_busq_avanzada);
			     button2.setOnClickListener(busq_avanzada);
			     button3= (ImageButton)findViewById(R.id.button_busq_mapa);
			     button3.setOnClickListener(busq_mapa);
			      
			     txtInput = (EditText)findViewById(R.id.main_text_input);
			     txtInput.setOnClickListener(clic_text_input);
			     txtInput.setText(input);
			     
			 }
		} catch (IOException e) {
			setContentView(R.layout.main);
			Toast.makeText(getBaseContext(),"Service down", Toast.LENGTH_SHORT).show();
			TextView t = new TextView(getBaseContext());
			t.setGravity(0x01);
			t.setText("Service down"); 
			LinearLayout l = (LinearLayout)findViewById(R.id.layoutButton); 
			l.addView(t);
			
			
			button1 = (ImageButton)findViewById(R.id.button_busq_simple);
		    button2 = (ImageButton)findViewById(R.id.button_busq_avanzada);
		    button3= (ImageButton)findViewById(R.id.button_busq_mapa);
		    txtInput = (EditText)findViewById(R.id.main_text_input);
			
			button1.setEnabled(false);
			button2.setEnabled(false);
			button3.setEnabled(false);
			txtInput.setEnabled(false);
			
		}
    	
    }
    //guardamos los datos de estado para que cuando se inicialize el metodo
    //onCreate puedan ser usados
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("INPUT_STRING", this.txtInput.getText().toString()); // adding a string in bundle
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	//agrega los menus directamente desde main_menu.xml
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_ayuda:
        	Intent intent2 = new Intent(DBPediaMobile.this, AyudaLista.class);
	        startActivity(intent2); 
        	break;
       /* case R.id.menu_ir_mapas:
    		Intent intent = new Intent(DBPediaMobile.this, Mapa.class);
	        startActivity(intent); 
        	break;
        case R.id.menu_config:
        	break;
       
        case R.id.menu_home:
        	setContentView(R.layout.main);
			button1 = (ImageButton)findViewById(R.id.button_busq_simple);
            // Register the onClick listener with the implementation above
            button1.setOnClickListener(busq_simple);
            button2 = (ImageButton)findViewById(R.id.button_busq_avanzada);
            button2.setOnClickListener(busq_avanzada);
            button3= (ImageButton)findViewById(R.id.button_busq_mapa);
            button3.setOnClickListener(busq_mapa);
                    
            txtInput = (EditText)findViewById(R.id.main_text_input);
            txtInput.setOnClickListener(clic_text_input);
        	break;
        */
        }
        return false;
    }
 
    //oyente para el boton de busqueda simple
    private OnClickListener busq_simple = new OnClickListener() {
        public void onClick(View v) {
        	Intent intent = new Intent(DBPediaMobile.this, BusquedaSimple.class);
			startActivity(intent); 
        }
    };
    //oyente para el boton de busqueda avanzada
    private OnClickListener busq_avanzada = new OnClickListener() {
        public void onClick(View v) {
			Intent intent = new Intent(DBPediaMobile.this, BusquedaAvanzada.class);
			startActivity(intent);
        }
    };
   //oyente para el boton de busqueda en mapa
    private OnClickListener busq_mapa= new OnClickListener() {    	
    	public void onClick(View v) {
    		bip=null;	
    		Intent intent = new Intent(DBPediaMobile.this, Mapa.class);
	        startActivity(intent); 
        }
       
    };
    

   //oyente para el boton de busqueda 
    private OnClickListener clic_text_input= new OnClickListener() {
        public void onClick(View v) {        	
        }
    };
    
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	} 
    
}
